package com.dk.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@EnableAsync
@Configuration
public class ThreadPoolController {

    @Bean
    public Executor taskExecutor(){
        /** spring 提供的线程池*/
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        /** ==========  背下来 ，线程的核心参数
         *  */
        /* 设置核心工作线程数*/
        executor.setCorePoolSize(10);
        /* 设置最大线程数*/
        executor.setMaxPoolSize(100);
        /* 设置等待队列数*/
        executor.setQueueCapacity(100);
        /* 设置线程池的拒接策略*/
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        /* 设置线程的前缀名*/
        executor.setThreadNamePrefix("thread-service");
        /* 初始化线程*/
        executor.initialize();

        return executor;
    }
}
